//
//  RefuseResidencePermitViewController.m
//  Jyxt
//
//  Created by 王炜光 on 2017/8/21.
//  Copyright © 2017年 DaLian Rental Housing Management. All rights reserved.
//

#import "RefuseResidencePermitViewController.h"
#import "ResidencePermitViewModel.h"
#import "UserCenterViewModel.h"
#import <LemonBubble.h>
@interface RefuseResidencePermitViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)NSArray *reasonArr;

@property (nonatomic, strong)NSMutableArray *selectedReasonArr;

@property (nonatomic, strong)UITableView *rootTableView;

@end

@implementation RefuseResidencePermitViewController

//- (void)initView{
//    
//    self.view.backgroundColor = LKColorMake(245, 245, 245, 1);
//    
//    _reasonArr = @[@"照片不清晰",@"身份证不合格",@"申请材料不合格",@"其他原因"];
//    _selectedReasonArr = [[NSMutableArray alloc] init];
//    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 15+64, SCREEN_WIDTH, 30)];
//    titleLabel.text = @"请选择不合格理由(可多选)";
//    titleLabel.textColor = LKColorMake(171, 171, 171, 1);
//    titleLabel.font = [UIFont systemFontOfSize:13];
//    [self.view addSubview:titleLabel];
//    
//    
//    UIView *whiteBGView = [[UIView alloc]initWithFrame:CGRectMake(0, 45+64, SCREEN_WIDTH, 160)];
//    whiteBGView.backgroundColor = [UIColor whiteColor];
//    [self.view addSubview:whiteBGView];
//    for (NSInteger i = 0; i < _reasonArr.count; i++) {
//        UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        selectBtn.frame = CGRectMake(15, 10 +i*40, 20, 20);
//        selectBtn.tag = 1000+i;
//        [selectBtn setImage:[UIImage imageNamed:@"rp_select"] forState:UIControlStateNormal];
//        [selectBtn addTarget:self action:@selector(selectedBtnAction:) forControlEvents:UIControlEventTouchUpInside];
//        [whiteBGView addSubview:selectBtn];
//        
//        UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 10+i*40, SCREEN_WIDTH-65, 20)];
//        tempLabel.text = _reasonArr[i];
//        tempLabel.font = [UIFont systemFontOfSize:15];
//        [whiteBGView addSubview:tempLabel];
//        
//        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(15, 40 + i*40, SCREEN_WIDTH-15, 1)];
//        lineView.backgroundColor = self.view.backgroundColor;
//        [whiteBGView addSubview:lineView];
//    }
//    
//    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [submitBtn setBackgroundColor:LKColorMake(100,131,228, 1)];
//    submitBtn.titleLabel.textColor = [UIColor whiteColor];
//    submitBtn.frame = CGRectMake(15, 225+64, SCREEN_WIDTH-30, 45);
//    [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
//    submitBtn.layer.cornerRadius = 5;
//    [submitBtn addTarget:self action:@selector(refuseAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:submitBtn];
//}
//
- (void)refuseAction:(UIButton *)btn{
    if (_selectedReasonArr.count > 0) {
        NSString *reason = [_selectedReasonArr componentsJoinedByString:@","];
        [self showRoundProgressWithTitle:@"加载中..."];
        [ResidencePermitViewModel refusedResidencePermitApplyWithData:@{@"utoken":[UserCenterViewModel getLocalUToken],@"id":self.valueDic[@"id"],@"reason":reason} success:^(id response) {
            [self hideBubble];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"审核完成，您已拒绝此条居住证办理申请，可在“已拒绝”中查看。" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self.navigationController popToRootViewControllerAnimated:YES];
                
            }];
            [alert addAction:sureAction];
            [self presentViewController:alert animated:YES completion:nil];
            
        } failed:^(NSString *reason) {
            
            [self showErrorWithTitle:reason autoCloseTime:3];
        }];
        
    }else{
       [self showErrorWithTitle:@"请先选择拒绝原因" autoCloseTime:3];
    }
}
//
//- (void)selectedBtnAction:(UIButton *)btn{
//    btn.selected = !btn.selected;
//    if (btn.selected) {
//        [btn setImage:[UIImage imageNamed:@"rp_selected"] forState:UIControlStateNormal];
//        [_selectedReasonArr addObject:_reasonArr[btn.tag-1000]];
//    }else{
//        [btn setImage:[UIImage imageNamed:@"rp_select"] forState:UIControlStateNormal];
//        [_selectedReasonArr removeObject:_reasonArr[btn.tag-1000]];
//    }
//}



/*================================================================*/

- (void)initView{
    
    self.view.backgroundColor = LKColorMake(245, 245, 245, 1);
    _reasonArr = @[@"照片不清晰",@"身份证不合格",@"申请材料不合格",@"其他原因"];
    _selectedReasonArr = [[NSMutableArray alloc] init];
    _rootTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, SCREEN_HEIGHT-44) style:UITableViewStylePlain];
    _rootTableView.backgroundColor = LKColorMake(245, 245, 245, 1);
    _rootTableView.delegate = self;
    _rootTableView.dataSource = self;
    [self.view addSubview:_rootTableView];
    
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, 45)];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, SCREEN_WIDTH, 30)];
    titleLabel.text = @"请选择不合格理由(可多选)";
    titleLabel.textColor = LKColorMake(171, 171, 171, 1);
    titleLabel.font = [UIFont systemFontOfSize:13];
    [headerView addSubview:titleLabel];
    
    _rootTableView.tableHeaderView = headerView;
    
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, 65)];
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [submitBtn setBackgroundColor:LKColorMake(100,131,228, 1)];
    submitBtn.titleLabel.textColor = [UIColor whiteColor];
    submitBtn.frame = CGRectMake(15, 20, SCREEN_WIDTH-30, 45);
    [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    submitBtn.layer.cornerRadius = 5;
    [submitBtn addTarget:self action:@selector(refuseAction:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:submitBtn];
    
    _rootTableView.tableFooterView = footerView;
    
    [_rootTableView setEditing:YES animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    cell.textLabel.text = _reasonArr[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _reasonArr.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [_selectedReasonArr addObject:_reasonArr[indexPath.row]];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    [_selectedReasonArr removeObject:_reasonArr[indexPath.row]];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    //多选的编辑模式
        return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
